﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Xml;
using Microsoft.Office.PowerPoint.STAMP.Command;
using Microsoft.Office.PowerPoint.STAMP.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Office.PowerPoint.STAMP.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class CaptionExporterTest
    {
        #region expectedXML
        private const string _expectedXMLText =
            @"<?xml version='1.0' encoding='UTF-8'?>"
            + @"<tt xmlns=""http://www.w3.org/ns/ttml"" xmlns:tts=""http://www.w3.org/ns/ttml#styling"" xml:lang=""en"">"
            + @"  <head>                                                                                          "
            + @"    <styling xmlns:tts=""http://www.w3.org/ns/ttml#styling"">                                       "
            + @"      <style xml:id=""Style1""                                                                      "
            + @"        tts:color=""#FFFFFF""                                                                       "
            + @"        tts:backgroundColor=""#00FFFF""                                                             "
            + @"        tts:fontFamily=""Calibri""                                                                  "
            + @"        tts:fontSize=""12em""                                                                       "
            + @"        tts:textAlign=""center""                                                                    "
            + @"        tts:fontWeight=""bold""/>                                                                   "
            + @"      <style xml:id=""Style2""                                                                      "
            + @"        tts:color=""#00FF00""                                                                       "
            + @"        tts:backgroundColor=""#FF0000""                                                              "
            + @"        tts:fontFamily=""Tahoma""                                                                   "
            + @"        tts:fontSize=""22em""                                                                       "
            + @"        tts:textAlign=""left""                                                                      "
            + @"        tts:fontStyle=""italic""                                                                    "
            + @"        tts:textDecoration=""underline""/>                                                          "
            + @"    </styling>                                                                                    "
            + @"  </head>                                                                                         "
            + @"  <body>                                                                                          "
            + @"    <div xml:lang=""en"">                                                                           "
            + @"      <p style=""Style1"" begin=""00:00:01.2"" end=""00:00:02.0"">one to two</p>                        "
            + @"      <p style=""Style2"" begin=""00:00:02.0"" end=""00:00:03.0"">two to three</p>                      "
            + @"      <p style=""Style1"" begin=""00:00:03.0"" end=""00:00:04.0"">three to four</p>                     "
            + @"      <p begin=""00:00:03.0"" end=""00:00:04.0"">four to five</p>                                     "
            + @"    </div>                                                                                        "
            + @"  </body>                                                                                         "
            + @"</tt>                                                                                             ";
        #endregion
        private TestContext _testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return _testContextInstance;
            }
            set
            {
                _testContextInstance = value;
            }
        }

        [TestMethod]
        public void Exports()
        {
            IEnumerable<CaptionItem> captionItems = BuildCaptionItems();
            CaptionExporter exporter = new CaptionExporter(captionItems);
            string xmlContent = exporter.ExportToTTML();

            AssertTTMLEqual(xmlContent, _expectedXMLText);
        }

        private static IEnumerable<CaptionItem> BuildCaptionItems()
        {
            List<CaptionItem> items = new List<CaptionItem>
                                          {
                                              BuildCaption(
                                                  new TimeSpan(0, 0, 0, 1, 200),
                                                  new TimeSpan(0, 0, 0, 2, 0),
                                                  "one to two",
                                                  ColorTranslator.FromHtml("#FFFFFF"), 
                                                  ColorTranslator.FromHtml("#00FFFF"),
                                                  "Calibri",
                                                  new Length {Unit = LengthUnit.Point, Value = 12},
                                                  FontStyle.Bold, TextAlignAttibuteValue.center),
                                              BuildCaption(
                                                  new TimeSpan(0, 0, 0, 2, 0),
                                                  new TimeSpan(0, 0, 0, 3, 0),
                                                  "two to three",
                                                  ColorTranslator.FromHtml("#00FF00"), 
                                                  ColorTranslator.FromHtml("#FF0000"),
                                                  "Tahoma",
                                                  new Length {Unit = LengthUnit.Point, Value = 22},
                                                  FontStyle.Italic | FontStyle.Underline, TextAlignAttibuteValue.left),
                                              BuildCaption(
                                                  new TimeSpan(0, 0, 0, 3, 0),
                                                  new TimeSpan(0, 0, 0, 4, 0),
                                                  "three to four",
                                                  ColorTranslator.FromHtml("#FFFFFF"), 
                                                  ColorTranslator.FromHtml("#00FFFF"),
                                                  "Calibri",
                                                  new Length {Unit = LengthUnit.Point, Value = 12},
                                                  FontStyle.Bold, TextAlignAttibuteValue.center),
                                              BuildCaption(
                                                  new TimeSpan(0, 0, 0, 3, 0),
                                                  new TimeSpan(0, 0, 0, 4, 0),
                                                  "four to five")
                                          };
            return items;
        }

        private static CaptionItem BuildCaption(TimeSpan beginTime, TimeSpan endTime, string text, Color fontColor, Color backgroundColor, string fontName, Length fontSize, FontStyle fontStyle, TextAlignAttibuteValue textAlign)
        {
            CaptionStyle captionStyle = new CaptionStyle
                                            {
                                                BackgroundColor = backgroundColor,
                                                FontColor = fontColor,
                                                FontName = fontName,
                                                FontSize = fontSize,
                                                FontStyle = fontStyle,
                                                TextAlignStyleProperty = textAlign
                                            };
            return BuildCaption(beginTime, endTime, text, captionStyle);
        }

        private static CaptionItem BuildCaption(TimeSpan beginTime, TimeSpan endTime, string text, CaptionStyle captionStyle = null)
        {
            return new CaptionItem
                       {
                           BookMarkEntryPosition = (int)beginTime.TotalMilliseconds,
                           BookMarkExitPosition = (int)endTime.TotalMilliseconds,
                           Text = text,
                           CaptionStyle = captionStyle
                       };
        }

        private static void AssertTTMLEqual(string actual, string expected)
        {
            XmlDocument actualXmlDocument = BuildXmlDocument(actual);
            XmlDocument expectedXmlDocument = BuildXmlDocument(expected);

            Assert.AreEqual(expectedXmlDocument.OuterXml, actualXmlDocument.OuterXml);
        }

        /// <summary>
        /// Builds the XML document.
        /// </summary>
        /// <param name="xmlText">The XML text.</param>
        /// <returns></returns>
        private static XmlDocument BuildXmlDocument(string xmlText)
        {
            XmlDocument expectedXmlDocument = new XmlDocument();
            expectedXmlDocument.LoadXml(xmlText);
            return expectedXmlDocument;
        }
    }
}
